<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品增加</title>
		<link type="text/css" rel="stylesheet" href="css/tableAndPage.css" />
	</head>
	<body>
	<!-- 	showType1Date: "",
		showType2Date: "",
		showType3Date: "",
		name: "",
		a_picPath: "",
		price: "",
		stock: "", -->
		<h1>商品增加</h1>
		<div id="app">
		  <form id="forms" name="forms" enctype="multipart/form-data" method="post">
			  添加分类:<br />
			  一级分类:<select name="categoryLevel1Id" v-model="type1" @change="showProTypeId2(type1)">
			  	<option v-for="item1 in proType1" v-bind:value="item1.id">{{item1.name}}</option>
			  </select><br>
			 二级分类:<select name="categoryLevel2Id" v-model="type2" @change="showProTypeId3(type2)">
			  	<option v-for="item2 in proType2" v-bind:value="item2.id">{{item2.name}}</option>
			  </select><br>
			 三级分类:<select name="categoryLevel3Id" v-model="type3">
                 // <option v-for="item3 in proType3" v-bind:value="item3.id">{{item3.name}}</option>
             </select><br><br>
			 商品名:<input name="name" type="text" v-model="name" style="width: 120px; height: 40px;" />
              <br><br>
			商品图片:<input name="a_picPath" type="file" id="pic" /><br />
              价格:<input name="price" type="text" v-model="price" style="width: 120px;" /><br />
              库存:<input name="stock" type="text" v-model="stock" style="width: 120px;" /><br />
              <br />
          </form>
			<button @click="addProduct()" type="button">确认</button>&nbsp;&nbsp;&nbsp;
			<button @click="back()">取消</button>
		</div>
	</body>
</html>
<!-- serialNumber -->
<script src="js/axios.js"></script>
<script src="js/md5.js"></script>
<script src="js/vue.js"></script>
<script src="js/jquery-1.11.1.min_044d0927.js"></script>
<script>
	var app = new Vue({
		el:"#app",
		data:{
			proType1:[],
			proType2:[],
			proType3:[],
			type1: "0",
			type2: "0",
			type3: "0",
			name: "",
			a_picPath: "",
			price: "",
			stock: "",
		},methods:{
			md5Public:async function(objectStr){
				let randomNum = 0;
				//接口拿随机数
				await axios({
					url:"http://localhost:8080/setRedisNum",
					methods:"get"
				}).then(res => {
					// alert("随机数开始");
					console.log("随机数:"+res.data);
					randomNum = res.data;

				})
				//放到header里的参数 时间戳 随机数 加密的字符串
				//时间戳
				var dateStr = new Date();
				dateStr = Date.now();

				//把obj按照key值升序
				let sortObject = function(obj){
					let sorter = function(a,b){
						return obj[a] - obj[b];
					}
					let keys = Object.keys(obj);
					keys.sort(sorter);
					let res = {};
					keys.forEach(key => {
						res[key] = obj[key];
					});
					return res;
				}
				//升序的对象
				var sxStr = sortObject(objectStr);
				//把升序的对象转成字符串
				var jsonStr = "";
				for(let obj in objectStr){
					jsonStr += obj+"="+objectStr[obj]+"&";
				}
				//把最后一个&符号切割了
				jsonStr = jsonStr.substr(0, jsonStr.length - 1);

				//加密字符串
				var str = MD5(jsonStr+randomNum);
				console.log("jsonStr:"+jsonStr);
				console.log("randomNum:"+randomNum);
				// alert("str:"+str);
				await axios({
					url:"http://localhost:8080/returnInterfaceStr",
					methods:"POST",
					params:objectStr,
					headers:{
						"TimeStamp":dateStr,
						"randomNum":randomNum,
						"encryptionStr":str
					}
				}).then(res => {
					console.log(res);
					console.log("后端接受之:"+res.data);
					if( res.data == true ){
						return true;
					}else{
						return false;
					}
				})
				return true;
			},
         showProTypeId1:async function (parentId){
				// alert(2)
			 var that = this;
			 let flag = await this.md5Public({"a1":54,"a2":55,"a3":56});
			 if( flag == true ){
				 await axios ({
					 method: 'post',
					 url:"http://localhost:8080/getProductTypes",
					 headers: { 'content-type': 'application/x-www-form-urlencoded' },
					 data:{
						 typeId:1,
						 parentId:parentId,
					 }
				 }).then(function (response) {
					 console.log(response.data.data)
					 that.proType1=response.data.data;
					 // console.log(that.proType1);
				 })
			 }
		 },
			showProTypeId2:async function(parentId){
				var that = this;
				let flag = await this.md5Public({"a1":54,"a2":55,"a3":56});
				if( flag == true ){
					await axios({
						method: 'post',
						url:"http://localhost:8080/getProductTypes",
						headers: { 'content-type': 'application/x-www-form-urlencoded' },
						data:{
							typeId:2,
							parentId:parentId,
						}
					}).then(function (response) {
						console.log(response.data.data);
						that.proType2=response.data.data;
					})
				}
			},
			showProTypeId3:async function(parentId){
				var that = this;
				let flag = await this.md5Public({"a1":54,"a2":55,"a3":56});
				if( flag == true ){
					await axios({
						method: 'post',
						url:"http://localhost:8080/getProductTypes",
						headers: { 'content-type': 'application/x-www-form-urlencoded' },
						data:{
							typeId:3,
							parentId:parentId,
						}
					}).then(function (response) {
						console.log(response.data.data);
						that.proType3=response.data.data;
					})
				}
			},
			addProduct:async function(){
				var picPath= $("#pic").val();
				var index = picPath.lastIndexOf(".")+1;
				var hzPicPath = picPath.substring(index);
				if(hzPicPath!="jpg"){
					alert("请jpg格式的文件");
					return;
				}
				if(this.type1=='0'){
					alert("请选择分类1")
					return;
				}
				if(this.type2=='0'){
					alert("请选择分类2")
					return;
				}
				if(this.type3=='0'){
					alert("请选择分类3")
					return;
				}
				if(this.name==''){
					alert("请输入商品名")
					return;
				}
				if(this.price==''){
					alert("请输入价格")
					return;
				}
				if(isNaN(this.price)){
					alert("请输入数字");
					return;
				}
				if(this.stock==''){
					alert("请输入库存")
					return;
				}
				if(isNaN(this.stock)){
					alert("请输入数字");
					return;
				}
				await axios({
					method:"post",
					url:"http://localhost:8080/addProduct",
					Headers:{
						'Content-Type':'multipart/form-data'
					},
					data:new FormData(document.getElementById('forms'))
				}).then(res=>{
					if(res.data.data>0){
						alert("增加成功");
						window.location="goodsList.html";
					}else{
						alert("增加失败");
					}
				})
			},
			back:function () {
				// window.location.href="goodsList.html"
			}
		},
		created(){
			let storage = window.localStorage;
			// alert(1);
			this.showProTypeId1(0);
			axios({
					url:"http://localhost:8080/UploginStatus?userName="+storage.getItem("userName"),
					methods:"GET"
			}).then(res => {
					if( res.data.code == 500 ){
							storage.clear();
					}
			})
		}
	})

	/*var app = new Vue({
		el: "#app",
		data: {
			id: decodeURI(window.location.href).split("=")[1],
			type1: "0",
			type2: "0",
			type3: "0",
			showType1Date: "",
			showType2Date: "",
			showType3Date: "",
			name: "",
			a_picPath: "",
			price: "",
			stock: "",
		},
		methods: {
			showTypeById1: function(typeId) {
				// alert("注册用户");
				// alert(typeId);
				var that = this;
				axios.get("http://localhost:8080/productCategory/showProductCategoryByType?type=" + typeId)
					.then(
						function(response) {
							console.log(response.data.data);
							// that.type1=response.data.data.
							that.showType1Date = response.data.data;
						},
						function(err) {
							alert(出错了);
						}
					)
			},
			showTypeById2: function(parentId) {
				// alert("注册用户");
				// alert(parentId);
				var that = this;
				axios.get("http://localhost:8080/productCategory/showProductCategory2ByType?type=2" +
					"&parentId=" + parentId).then(
					function(response) {
						console.log(response.data.data);
						that.showType2Date = response.data.data;
					},
					function(err) {
						alert(出错了);
					}
				)
			},
			showTypeById3: function(parentId) {
				// alert("注册用户");
				// alert(parentId);
				var that = this;
				axios.get("http://localhost:8080/productCategory/showProductCategory2ByType?type=3" +
					"&parentId=" + parentId).then(
					function(response) {
						console.log(response.data.data);
						that.showType3Date = response.data.data;
					},
					function(err) {
						alert(出错了);
					}
				)
			},
			addProduct:function(){
				var picPath= $("#pic").val();
				var index = picPath.lastIndexOf(".")+1;
				var hzPicPath = picPath.substring(index);
				if(hzPicPath!="jpg"){
					alert("请jpg格式的文件");
					return;
				}
				if(this.type1=='0'){
					alert("请选择分类1")
					return;
				}
				if(this.type2=='0'){
					alert("请选择分类2")
					return;
				}
				if(this.type3=='0'){
					alert("请选择分类3")
					return;
				}
				if(this.name==''){
					alert("请输入商品名")
					return;
				}
				if(this.price==''){
					alert("请输入价格")
					return;
				}
				if(isNaN(this.price)){
					alert("请输入数字");
					return;
				}
				if(this.stock==''){
					alert("请输入库存")
					return;
				}
				if(isNaN(this.stock)){
					alert("请输入数字");
					return;
				}
				axios({
					method:"post",
					url:"http://localhost:8080/product/addProductToSql",
					Headers:{
						'Content-Type':'multipart/form-data'
					},
					data:new FormData(document.getElementById('forms'))
				}).then(res=>{
					if(res.data.data>0){
						alert("增加成功")
						window.location="productShow.html";
					}else{
						alert("增加失败");
					}
				})
			},
			qx:function(){
				window.location="productShow.html"
			}

			
		},
		created() {
			this.showTypeById1(1);
		},

	})*/
</script>
